<template>
  <div class="order-payment-container">
    <!-- 筛选区域 -->
    <div class="filter-section">
      <el-input
        v-model="searchQuery"
        placeholder="输入订单号或描述搜索"
        style="width: 250px; margin-right: 20px;"
        clearable
        @clear="fetchOrders"
      />
      <el-select
        v-model="filterType"
        placeholder="筛选类型"
        style="width: 150px; margin-right: 20px;"
        clearable
        @change="fetchOrders"
      >
        <el-option label="全部" value="" />
        <el-option label="充值" value="充值" />
        <el-option label="提现" value="提现" />
      </el-select>
      <el-button type="primary" @click="fetchOrders">刷新</el-button>
    </div>

    <!-- 订单表格 -->
    <el-table
      :data="filteredOrders"
      style="width: 100%; margin-top: 20px;"
      stripe
      :header-cell-style="{ color: '#333' }"
    >
      <el-table-column prop="orderNum" label="订单号" sortable />
      <el-table-column prop="type" label="类型" sortable />
      <el-table-column prop="des" label="描述" />
      <el-table-column
        prop="createdAt"
        label="生成时间"
        sortable
      >
        <template #default="scope">
          {{ formatDate(scope.row.createdAt) }}
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import axiosService from '@/utils/request'
import { showMsg } from '@/utils/showMsg'

export default {
  name: 'OrderPayment',
  data()
  {
    return {
      orders: [], // 原始订单数据
      searchQuery: '', // 搜索关键词
      filterType: '', // 筛选类型
      formatter: new Intl.DateTimeFormat('zh-CN', {
        year: 'numeric',
        month: '2-digit',
        day: '2-digit',
        hour: '2-digit',
        minute: '2-digit',
        second: '2-digit',
        hour12: false // 使用 24 小时制
      })
    }
  },
  computed: {
    // 过滤后的订单数据
    filteredOrders()
    {
      return this.orders.filter(order =>
      {
        const matchesSearch = !this.searchQuery ||
          order.orderNum.includes(this.searchQuery) ||
          order.des.includes(this.searchQuery)
        const matchesType = !this.filterType || order.type === this.filterType
        return matchesSearch && matchesType
      })
    }
  },
  created()
  {
    this.fetchOrders()
  },
  methods: {
    // 获取订单数据
    fetchOrders()
    {
      axiosService.get(`/node/ordpays/getSome/id/${this.$store.state.user.id}`)
        .then(res =>
        {
          this.orders = res.data
        })
        .catch(e =>
        {
          showMsg(e, null, 'error')
        })
    },

    // 格式化日期为 YYYY-MM-DD HH:MM:SS
    formatDate(dateStr)
    {
      return this.formatter.format(new Date(dateStr))
    }
  }
}
</script>

<style scoped>
.order-payment-container {
  user-select: none;
  padding: 20px;
  height: calc(100vh - 190px);
  overflow: hidden;
  margin-left: 100px;
  color: #333; /* 深灰色文字，白色背景下更明显 */
}

.filter-section {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 20px;
}
</style>
